/*********************************************************************************
 *      Copyright:  (C) 2025 ZYT
 *                  All rights reserved.
 *
 *       Filename:  main.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(10/08/25)
 *         Author:  ZYT <zhangyuting@163.com>
 *      ChangeLog:  1, Release initial version on "10/08/25 09:28:16"
 *                 
 ********************************************************************************/
#include "main.h"
#include "can_comm.h"
#include "bridge.h"

// 外部线程函数
extern void* thread1_func(void* arg);
extern void* thread2_func(void* arg);

int main() 
{
	pthread_t tid1, tid2;


	// 1.初始化CAN
	if (can_comm_init() < 0) 
	{
		fprintf(stderr, "[Main] CAN init failed!\n");
		return -1;
	}

	// 2.初始化并启动Bridge线程
	if (bridge_init() < 0) 
	{
		fprintf(stderr, "[Main] Bridge init failed!\n");
		can_comm_close();
		return -1;
	}

	// 3.创建thread1 和thread2
	if (pthread_create(&tid1, NULL, thread1_func, NULL) != 0) 
	{
		fprintf(stderr, "[Main] Failed to create Thread1.\n");
		goto cleanup;
	}

	if (pthread_create(&tid2, NULL, thread2_func, NULL) != 0) 
	{
		fprintf(stderr, "[Main] Failed to create Thread2.\n");
		goto cleanup;
	}

	printf("[Main] All 3 threads started: Bridge, Thread1, Thread2.\n");

	// 4. 等待线程结束（实际中可 Ctrl+C 退出）
	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);

cleanup:
	bridge_destroy();
	can_comm_close();
	printf("[Main] Application exited.\n");
	return 0;
}
